package com.lin.lazybone.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.lazybone.MainActivity;
import com.lin.lazybone.R;
import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.utils.ShareUtils;
import com.lin.lazybone.utils.StaticClass;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends BaseActivity
{
    private EditText account;
    private EditText password;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //判断用户是否已经登陆
        _User user = BmobUser.getCurrentUser(_User.class);
        if(user != null){
            // 允许用户使用应用
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }

        TextView forget = (TextView) findViewById(R.id.forgetPassword);
        forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ForgetActivity.class);
                startActivity(intent);
            }
        });

        TextView signIn = (TextView) findViewById(R.id.newLogin);
        signIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, SignActivity.class);
                startActivity(intent);
            }
        });

       //登录
        Button login = (Button) findViewById(R.id.loginButton);
        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public  void onClick(View view){
                final CustomDialog dialog = new CustomDialog(LoginActivity.this, 100, 400, R.layout.dialog_loding,
                        R.style.Theme_dialog, Gravity.CENTER,R.style.pop_anim_style);
                dialog.setCancelable(false);
                dialog.show();
                account = (EditText)findViewById(R.id.accountText);
                password = (EditText)findViewById(R.id.passwordText);
                String account_tmp = account.getText().toString();
                String password_tmp = password.getText().toString();
                _User user = new _User();
                user.loginByAccount(account_tmp,password_tmp,new LogInListener<_User>() {
                    @Override
                    public void done(_User _user,BmobException e){
                        if(e == null){
                            dialog.dismiss();
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        }else{
                            dialog.dismiss();
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });





    }
}
